<?php

    class Login extends CI_Controller
    {

        function __construct()
        {
            parent::__construct();

            $this->load->library('form_validation');
            $this->load->library('simple_login');

        }

        function index()
        {
            $this->view_loginform();

        }

        function view_loginform()
        {
            if($this->session->userdata('logged_in'))
            {
                redirect(base_url());
            }
            else
            {
                $this->load->view('login/loginform_new');
            }

        }

        function view_newuserform()
        {
            $this->load->view('login/newuserform');

        }

        function loginuser()
        {

            if($this->validloginform())
            {
                
                if($this->simple_login->login($_POST['email'], $_POST['password']))
                {
                    $this->session->set_userdata('my_language', 'me');
                    $this->view_loginform();
                }
                else
                {
                    $data['logerror']='Please enter the correct login information.';
                    $this->load->view('login/loginform_new', $data);
                }
            }
            else
            {
                $this->load->view('login/loginform_new');
            }

        }

        function newuser()
        {

            if($this->validnewuserform())
            {
                $this->simple_login->setNicename($this->input->post('nicename'));
                $this->simple_login->setEmail($this->input->post('email'));
                $this->simple_login->setPassword($this->input->post('password_one'));
                $this->simple_login->setAc_type("agency");

                if($this->simple_login->create())
                {
                    echo 'User added to DB.';
                }
                else
                {
                    echo 'DB Error!<br />User not added to DB.';
                }
            }
            else
            {
                $this->load->view('login/newuserform');
            }

        }

        function logout()
        {
            $this->simple_login->logout();
            redirect(base_url());

        }

        function validloginform()
        {

            $this->form_validation->set_rules('email', '<b>username</b>', 'trim|required');
            $this->form_validation->set_rules('password', '<b>password</b>', 'trim|required');

            $this->form_validation->set_message('required', 'Please enter %s!');
            $this->form_validation->set_message('valid_email', 'Field %s must be valid email address!');

            return $this->form_validation->run();

        }

        function validnewuserform()
        {

            $this->form_validation->set_rules('nicename', '<b>Niciname</b>', 'trim|xss_clean|required');
            $this->form_validation->set_rules('email', '<b>email</b>', 'trim|xss_clean|required');
            $this->form_validation->set_rules('password_one', '<b>password</b>', 'trim|xss_clean|required');
            $this->form_validation->set_rules('password_two', '<b>password again</b>', 'trim|xss_clean|required|matches[password_one]');

            $this->form_validation->set_message('required', 'Please enter %s!');
            $this->form_validation->set_message('valid_email', 'Field %s must be valid email address!');
            $this->form_validation->set_message('matches', '<b>Passwords</b> must mach!');

            return $this->form_validation->run();

        }

        function test_login()
        {
            if(!$this->session->userdata('logged_in'))
            {
                redirect(base_url().'login/view_loginform_new');
            }
            echo 'Only to see by logged user!';

        }

    }